package com.pro.springbootshop.service.impl;

import com.pro.springbootshop.entity.Buyer;
import com.pro.springbootshop.entityExtend.BuyerReturn;
import com.pro.springbootshop.mapper.BuyerMapper;
import com.pro.springbootshop.service.BuyerService;
import com.pro.springbootshop.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class BuyerServiceImpl implements BuyerService {
    public BuyerMapper buyerMapper;
    @Autowired
    TokenUtil tokenUtil;
    @Autowired
    public void setUserMapper (BuyerMapper buyerMapper) {
        this.buyerMapper = buyerMapper;
    }
    @Override
    public String saveBuyer(Buyer buyer) {
        if(buyerMapper.getBuyerByBuyerId(buyer.getBuyerId())==null){
            int i=buyerMapper.insert(buyer);
            if(i>0){
                return "注册成功";
            }else {
                return "注册失败";
            }
        }else {
            return "账户已存在";
        }
    }

    @Override
    public String loginBuyer(Buyer buyer) {
        Buyer newBuyer=buyerMapper.buyerLogin(buyer.getBuyerId(),buyer.getBuyerPw());
        if(newBuyer==null){
            return null;
        }else{
            return tokenUtil.getToken(newBuyer);
        }
    }

    @Override
    public Map<String, String> getBuyer(String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        Buyer buyer=buyerMapper.getBuyerByBuyerId(buyerId);
        if(buyer==null){
            return null;
        }else {
            return tokenUtil.parseToken(token);
        }
    }

    @Override
    public String updateBuyer(Buyer buyer) {
        Buyer newBuyer=buyerMapper.getBuyerByBuyerId(buyer.getBuyerId());
        buyer.setBuyerPw(newBuyer.getBuyerPw());
        buyer.setId(newBuyer.getId());
        int i=buyerMapper.updateById(buyer);
        if(i>0){
            return tokenUtil.getToken(buyer);
        }else {
            return null;
        }
    }

    @Override
    public String updateAddress(String address, String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        Buyer buyer=buyerMapper.getBuyerByBuyerId(buyerId);
        String oldAddress=buyer.getBuyerAddress();
        String[] addressList=oldAddress.split(";");
        List<String> list1 = Arrays.asList(addressList);
        List<String> arrList = new ArrayList<String>(list1);
        arrList.remove(address);
        arrList.add(0,address);
        String newAddress=String.join(";",arrList);
        buyer.setBuyerAddress(newAddress);
        int i=buyerMapper.updateById(buyer);
        if(i>0){
            return tokenUtil.getToken(buyer);
        }else {
            return "修改失败";
        }
    }

    @Override
    public String changeAddress(String address, String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        Buyer buyer=buyerMapper.getBuyerByBuyerId(buyerId);
        buyer.setBuyerAddress(address);
        int i=buyerMapper.updateById(buyer);
        if(i>0){
            return tokenUtil.getToken(buyer);
        }else {
            return "修改失败";
        }
    }

    @Override
    public String updateBuyerPw(String oldBuyerPw, String buyerPw, String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        Buyer buyer=buyerMapper.buyerLogin(buyerId,oldBuyerPw);
        if(buyer!=null){
            buyerMapper.updatePassword(buyerPw,buyerId);
            return  "修改成功";
        }else {
            return "密码错误";
        }
    }

    @Override
    public BuyerReturn sellerGetBuyerIPage(Integer page, Integer limit) {
        int pageNo=(page-1)*limit;
        int total=buyerMapper.selectList(null).size();
        List<Buyer> buyerList= buyerMapper.sellerGetBuyer(pageNo,total);
        for(Buyer b:buyerList){
            b.setBuyerPw("");
        }
        return new BuyerReturn(buyerList,total);
    }

    @Override
    public List<Buyer> sellerGetBuyerList() {
        List<Buyer> buyerList= buyerMapper.selectList(null);
        for(Buyer b:buyerList){
            b.setBuyerPw("");
        }
        return buyerList;
    }

    @Override
    public String adminDeleteBuyer(String buyerId) {
        int i=buyerMapper.adminDeleteBuyer(buyerId);
        if(i>0){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }
}
